<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 7.1: Logistic regression</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch07_statistical_estim/html/logistics.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 7.1: Logistic regression</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 7.1.1</span>
<span class="comment">% Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Argyris Zymnis - 01/31/06</span>
<span class="comment">%</span>
<span class="comment">% We consider a binary random variable y with prob(y=1) = p and</span>
<span class="comment">% prob(y=0) = 1-p. We assume that that y depends on a vector of</span>
<span class="comment">% explanatory variables u in R^n. The logistic model has the form</span>
<span class="comment">% p = exp(a'*u+b)/(1+exp(a'*u+b)), where a and b are the model parameters.</span>
<span class="comment">% We have m data points (u_1,y_1),...,(u_m,y_m).</span>
<span class="comment">% We can reorder the data so that for u_1,..,u_q the outcome is y = 1</span>
<span class="comment">% and for u_(q+1),...,u_m the outcome is y = 0. Then it can be shown</span>
<span class="comment">% that the ML estimate of a and b can be found by solving</span>
<span class="comment">%</span>
<span class="comment">% minimize sum_{i=1,..,q}(a'*u_i+b) - sum_i(log(1+exp(a'*u_i+b)))</span>
<span class="comment">%</span>
<span class="comment">% In this case we have m = 100 and the u_i are just scalars.</span>
<span class="comment">% The figure shows the data as well as the function</span>
<span class="comment">% f(x) = exp(aml*x+bml)/(1+exp(aml*x+bml)) where aml and bml are the</span>
<span class="comment">% ML estimates of a and b.</span>

randn(<span class="string">'state'</span>,0);
rand(<span class="string">'state'</span>,0);

<span class="comment">% Generate data</span>
a =  1;
b = -5 ;
m= 100;

u = 10*rand(m,1);
y = (rand(m,1) &lt; exp(a*u+b)./(1+exp(a*u+b)));
plot(u,y,<span class="string">'o'</span>)
axis([-1,11,-0.1, 1.1]);

<span class="comment">% Solve problem</span>
<span class="comment">%</span>
<span class="comment">% minimize  -(sum_(y_i=1) ui)*a - b + sum log (1+exp(a*ui+b)</span>

U = [ones(m,1) u];
cvx_expert <span class="string">true</span>
cvx_begin
    variables <span class="string">x(2)</span>
    maximize(y'*U*x-sum(log_sum_exp([zeros(1,m); x'*U'])))
cvx_end

<span class="comment">% Plot results and logistic function</span>

ind1 = find(y==1);
ind2 = find(y==0);

aml = x(2);  bml = x(1);
us = linspace(-1,11,1000)';
ps = exp(aml*us + bml)./(1+exp(aml*us+bml));

dots = plot(us,ps,<span class="string">'-'</span>, u(ind1),y(ind1),<span class="string">'o'</span>,<span class="keyword">...</span>
     u(ind2),y(ind2),<span class="string">'o'</span>);

axis([-1, 11,-0.1,1.1]);
xlabel(<span class="string">'x'</span>);
ylabel(<span class="string">'y'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Successive approximation method to be employed.
   For improved efficiency, SDPT3 is solving the dual problem.
   SDPT3 will be called several times to refine the solution.
   Original size: 600 variables, 202 equality constraints
   200 exponentials add 1600 variables, 1000 equality constraints
-----------------------------------------------------------------
 Cones  |             Errors              |
Mov/Act | Centering  Exp cone   Poly cone | Status
--------+---------------------------------+---------
200/200 | 6.970e+00  2.483e+00  0.000e+00 | Solved
200/200 | 9.608e-01  7.319e-02  0.000e+00 | Solved
199/200 | 1.067e-01  8.487e-04  0.000e+00 | Solved
200/200 | 9.296e-03  6.366e-06  0.000e+00 | Solved
  0/127 | 3.203e-04  6.392e-09  0.000e+00 | Solved
-----------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -32.9797
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="logistics__01.png" alt=""> 
</div>
</div>
</body>
</html>